package com.dandev.cmdemo.web;

import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

import com.dandev.cmdemo.form.Contact;
import com.dandev.cmdemo.form.Communique;
import com.dandev.cmdemo.service.ContactService;

/**
 * Controller for the communique list view.  This displays a contact and a list of communiques.  
 * A new communique can be added from here and communiques can be deleted from here.  
 * An edit is a separate view/controller at the moment.
 *
 * @author Dan
 */
@Controller
@RequestMapping("/comm")
public class CommController {
	
	@Autowired
    private ContactService contactService;
	
	/**
	 * View communique list page, gets data for a contacts and all communiques.
	 * @param contactId
	 * @param model
	 */
	@RequestMapping(method=RequestMethod.GET)
	public void getEdit(@RequestParam(value = "contactId", defaultValue = "0") Long contactId, Model model) {
		Contact contact = contactService.getContact(contactId, true);
		
		Communique commForm = new Communique();
		commForm.setParentContactId(contactId);
		
		model.addAttribute("commForm", commForm);
		
		model.addAttribute("contact", contact);
	}
	
	/**
	 * Custom binder config, used for date formating.
	 * @param dataBinder
	 */
	@InitBinder
	public void initBinder(WebDataBinder dataBinder) {
	 
	SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy h:m a");
	sdf.setLenient(false);
	dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
	
	}
	
	/**
	 * Handles a post which will be a new communique.
	 * @param commForm new communique to insert
	 * @param result validation check
	 * @param model
	 * @return view name
	 */
	@RequestMapping(method=RequestMethod.POST)
	public String postEdit(@ModelAttribute("commForm") @Valid Communique commForm,  BindingResult result, Model model) {
		
		if (result.hasErrors()) {
			Long contactId = commForm.getParentContactId();
			Contact contact = contactService.getContact(contactId, true);
			model.addAttribute("commForm", commForm);
			model.addAttribute("contact", contact);
			
			return "comm";
		}
		
		
		contactService.addCommunique(commForm);
		
		return "redirect:/comm?contactId=" + commForm.getParentContactId();
	}
	
	/**
	 * Handles a delete communique request 
	 * @param contactId for redirection
	 * @param commId of the communique to be removed.
	 * @return
	 */
	@RequestMapping(value="/delete", method=RequestMethod.GET)
	public String doDelete(@RequestParam(value = "contactId") Long contactId, @RequestParam(value = "commId", defaultValue = "0") Long commId) {
		if(commId != 0) {
		
			contactService.removeCommunique(commId);
		}
		
		return "redirect:/comm?contactId=" + contactId;
	}

}
